package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.GroupHomeBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

public class GroupActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView rv_manager, rv_junior;
    private ImageView manager_info;
    private TextView boss_name;
    private TextView add_group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        initData();
        initTitle();
        initView();
        //  upLoad();
    }

    @Override
    protected void onResume() {
        upLoad();
        super.onResume();
    }

    private GroupHomeBean.InfoEntity infos = new GroupHomeBean.InfoEntity();

    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(Url.TEAM_COMPANY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        GroupHomeBean bean = gson.fromJson(result, GroupHomeBean.class);
                        infos = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setData();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private void initData() {

    }


    private void initView() {
        boss_name = (TextView) findViewById(R.id.boss_name);
        rv_manager = (RecyclerView) findViewById(R.id.rv_manager);
        rv_manager.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv_junior = (RecyclerView) findViewById(R.id.rv_junior);
        rv_junior.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        manager_info = (ImageView) findViewById(R.id.manager_info);
       // manager_info.setOnClickListener(this);
        add_group = (TextView) findViewById(R.id.add_group);
        add_group.setOnClickListener(this);

    }

    private void setData() {

        boss_name.setText(infos.getBoss().getRealname() + "");
        x.image().bind(manager_info, infos.getBoss().getHead_img());


        rv_manager.setAdapter(new BaseRVAdapter(this, infos.getManage()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_rv_manager;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                x.image().bind(holder.getImageView(R.id.head_img), infos.getManage().get(position).getHead_img());
                holder.setTextView(R.id.head_name, infos.getManage().get(position).getRealname());
            }
        });
        rv_junior.setAdapter(new BaseRVAdapter(this, infos.getTeam()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_rv_junior;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.setTextView(R.id.teamName, infos.getTeam().get(position).getTeamName());
                holder.setTextView(R.id.teamCounts, infos.getTeam().get(position).getTeamCounts());
                holder.getView(R.id.max_team).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(GroupActivity.this, GroupDetailsActivity.class).putExtra("teamId", infos.getTeam().get(position).getTeamId()));
                    }
                });
            }

        });
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("团队");
        //title.setTextSize(20);
        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("新建团队");
        submit.setTextColor(getResources().getColor(R.color.main_color));
        submit.setOnClickListener(this);
        submit.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                intent = new Intent(this, AddGroupActivity.class);
                startActivity(intent);
                break;
            case R.id.manager_info:
                intent = new Intent(this, GroupPersonInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.add_group:
                intent = new Intent(this, AddGotoGroupActivity.class);
                startActivity(intent);
                break;
        }
    }
}
